package com.xiaoshuidi.cloud.module.member.controller.admin.securitycheckprogram;

import com.xiaoshuidi.cloud.framework.security.core.util.SecurityFrameworkUtils;
import com.xiaoshuidi.cloud.module.member.convert.securitycheckprogram.MySecurityCheckProgramConvert;
import com.xiaoshuidi.cloud.module.member.enums.ApiConstants;
import com.xiaoshuidi.cloud.module.member.pojo.securitycheckprogram.SecurityCheckProgram;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import static com.xiaoshuidi.cloud.framework.common.pojo.CommonResult.success;

import com.xiaoshuidi.cloud.framework.excel.core.util.ExcelUtils;

import com.xiaoshuidi.cloud.framework.operatelog.core.annotations.OperateLog;
import static com.xiaoshuidi.cloud.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.xiaoshuidi.cloud.module.member.controller.admin.securitycheckprogram.vo.*;
import com.xiaoshuidi.cloud.module.member.convert.securitycheckprogram.SecurityCheckProgramConvert;
import com.xiaoshuidi.cloud.module.member.service.securitycheckprogram.SecurityCheckProgramService;

@Tag(name = "管理后台 - 安全检查项目")
@RestController
@RequestMapping(ApiConstants.NAME + "/security-check-program")
@Validated
public class SecurityCheckProgramController {

    @Resource
    private SecurityCheckProgramService securityCheckProgramService;
    @Resource
    private MySecurityCheckProgramConvert mySecurityCheckProgramConvert;

    @PostMapping("/create")
    @Operation(summary = "创建安全检查项目")
    @PreAuthorize("@ss.hasPermission('member:security-check-program:create')")
    public CommonResult<Long> createSecurityCheckProgram(@Valid @RequestBody SecurityCheckProgramCreateReqVO createReqVO) {
        return success(securityCheckProgramService.createSecurityCheckProgram(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新安全检查项目")
    @PreAuthorize("@ss.hasPermission('member:security-check-program:update')")
    public CommonResult<Boolean> updateSecurityCheckProgram(@Valid @RequestBody SecurityCheckProgramUpdateReqVO updateReqVO) {
        securityCheckProgramService.updateSecurityCheckProgram(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除安全检查项目")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:security-check-program:delete')")
    public CommonResult<Boolean> deleteSecurityCheckProgram(@RequestParam("id") Long id) {
        securityCheckProgramService.deleteSecurityCheckProgram(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得安全检查项目")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:security-check-program:query')")
    public CommonResult<SecurityCheckProgramRespVO> getSecurityCheckProgram(@RequestParam("id") Long id) {
        return success(securityCheckProgramService.getSecurityCheckProgram(id));
    }

    @GetMapping("/list")
    @Operation(summary = "获得安全检查项目列表")
    @PreAuthorize("@ss.hasPermission('member:security-check-program:query')")
    public CommonResult<List<SecurityCheckProgramRespVO>> getSecurityCheckProgramList(Boolean status) {
        List<SecurityCheckProgram> list = securityCheckProgramService.getSecurityCheckProgramList(status);
        return success(SecurityCheckProgramConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得安全检查项目分页")
    @PreAuthorize("@ss.hasPermission('member:security-check-program:query')")
    public CommonResult<PageResult<SecurityCheckProgramRespVO>> getSecurityCheckProgramPage(@Valid SecurityCheckProgramPageReqVO pageVO) {
        return success(securityCheckProgramService.getSecurityCheckProgramPage(pageVO));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出安全检查项目 Excel")
    @PreAuthorize("@ss.hasPermission('member:security-check-program:export')")
    @OperateLog(type = EXPORT)
    public void exportSecurityCheckProgramExcel(@Valid SecurityCheckProgramExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<SecurityCheckProgramRespVO> list = securityCheckProgramService.getSecurityCheckProgramList(exportReqVO);
        // 导出 Excel
        List<SecurityCheckProgramExcelVO> datas = mySecurityCheckProgramConvert.convert(list);
        ExcelUtils.write(response, "安全检查项目.xls", "数据", SecurityCheckProgramExcelVO.class, datas);
    }

    @GetMapping("/get-my-program")
    @Operation(summary = "获取我的项目列表")
    @PreAuthorize("@ss.hasPermission('member:security-check-program:query')")
    public CommonResult<List<GetMyProgramRespVO>> getMyProgram() {
        Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
        return success(securityCheckProgramService.getMyProgram(loginUserId));
    }

}
